package random.fox.p2p.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import flow.Flow;
import random.fox.p2p.App;
import random.fox.p2p.R;
import random.fox.p2p.event.ActionBarEvent;
import random.fox.p2p.event.GetPicEvent;
import random.fox.p2p.event.MenuClickEvent;
import random.fox.p2p.event.PickPicEvent;
import random.fox.p2p.event.ProgressEvent;
import random.fox.p2p.model.SendMessageModule;
import random.fox.p2p.presenter.SendMessagePresenter;
import random.fox.p2p.util.Util;
import random.fox.p2p.view.IMessageView;

/**
 * Created by wq on 15/3/24.
 */
public class SendMessageView extends LinearLayout implements IMessageView{
    @InjectView(R.id.toolbar)Toolbar toolbar;
    @InjectView(R.id.edit_message_content)
    EditText contentEditText;
    @InjectView(R.id.edit_message_title)
    EditText titleEditText;
    @InjectView(R.id.select_pic)
    ImageView selectPic;
    private File selectedImage;

    @Inject
    SendMessagePresenter presenter;

    public SendMessageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.inject(this);
        EventBus.getDefault().register(this);
        ((App)getContext().getApplicationContext())
                .createScopedGraph(new SendMessageModule(this)).inject(this);

        toolbar.setTitle("发表消息");
        EventBus.getDefault().post(new ActionBarEvent(toolbar,R.menu.menu_edit,true));
    }

    @OnClick(R.id.select_pic) void selectPic(){
            AlertDialog dialog =
                    new AlertDialog.Builder(getContext(),AlertDialog.THEME_DEVICE_DEFAULT_DARK).create();
            dialog.setTitle("选择图片");
            dialog.setButton(dialog.BUTTON_POSITIVE,"拍照",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    EventBus.getDefault().post(new PickPicEvent(Util.CAMERA));
                }
            });
            dialog.setButton(dialog.BUTTON_NEGATIVE,"相册选取",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    EventBus.getDefault().post(new PickPicEvent(Util.GALLERY));
                }
            });
            dialog.show();
    }

    public void onEvent(MenuClickEvent event){
        String theme = titleEditText.getText().toString().trim() ;
        String content = contentEditText.getText().toString().trim();
        presenter.addMessage(theme , content , selectedImage);
    }

    public void onEvent(GetPicEvent event){
        selectedImage = event.getPicFile();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                //.displayer(new CircleBitmapDisplayer())
                .build();
        ImageLoader.getInstance().displayImage(Uri.fromFile(selectedImage).toString(),selectPic,options);
    }


    @Override
    public void showProgress() {
        EventBus.getDefault().post(new ProgressEvent(true));

    }

    @Override
    public void hideProgress() {
        EventBus.getDefault().post(new ProgressEvent(false));
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDetachedFromWindow() {
        EventBus.getDefault().unregister(this);
        super.onDetachedFromWindow();
    }

    @Override
    public void finishView() {
        Flow.get(getContext()).goBack();
    }
}
